﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using zframe;
using System.Linq;

namespace zframe.editor {


    public class AssetBundleBuildUtil
    {
        private static string _abOutput = Path.Combine(Application.dataPath, "abBuilds");

        /*
        [MenuItem("zframe/AseetBundle/buildAll")]
        static void BuildAllABs()
        {
            BuildByTarget(BuildTarget.StandaloneWindows64 | BuildTarget.Android | BuildTarget.iOS | BuildTarget.StandaloneOSX);
        }
        */

        [MenuItem("zframe/AseetBundle/buildAndroid")]
        static void BuildAndroid()
        {
            BuildByTarget(BuildTarget.Android);
        }

        [MenuItem("zframe/AseetBundle/buildIOS")]
        static void BuildIOS()
        {
            BuildByTarget(BuildTarget.iOS);
        }

        [MenuItem("zframe/AseetBundle/buildWin64")]
        static void BuildWin64()
        {
            BuildByTarget(BuildTarget.StandaloneWindows64);
        }
        [MenuItem("zframe/AseetBundle/buildMacOS")]
        static void BuildOSX()
        {
            BuildByTarget(BuildTarget.StandaloneOSX);
        }

        [MenuItem("Assets/zframe/以文件夹为名对图片设置ab包")]
        static void BuildPicBySelectFolder()
        {
            string relativePath = AssetFileUtils.GetSelectFolder();

            BuildByFolderPath(relativePath, "atlas");
            EditorUtility.DisplayDialog("构建完成", $"构建AssetBundle完成!输出到={_abOutput}", "确定");
        }

        [MenuItem("Assets/zframe/以文件夹为名对.bytes设置ab包")]
        static void BuildConfigBySelectFolder()
        {
            string relativePath = AssetFileUtils.GetSelectFolder();
            BuildByFolderPath(relativePath, "config", new string[] { ".bytes" });
            EditorUtility.DisplayDialog("构建完成", $"构建AssetBundle完成!输出到={_abOutput}", "确定");
        }

        static void BuildByFolderPath(string relativePath, string fixName, string[] exts = null)
        {
            if (exts == null)
            {
                exts = new string[] { ".png", ".jpg" };
            }
            if (string.IsNullOrEmpty(relativePath) || !Directory.Exists(relativePath))
            {
                EditorUtility.DisplayDialog("参数错误", $"目录{relativePath}不存在", "确定");
                return;
            }
            string[] dirs = Directory.GetDirectories(relativePath);
            for (int i = 0; i < dirs.Length; i++)
            {
                BuildByFolderPath(dirs[i], fixName, exts);
            }
            string folder = FileUtils.GetDirLastFolderName(relativePath);
            string[] files = FileUtils.GetFilesByExts(relativePath, exts);
            //不需要对资源设置 assetBundleName 就可以打包成ab包
            AssetBundleBuild[] builds = new AssetBundleBuild[1];
            builds[0] = new AssetBundleBuild();
            builds[0].assetNames = files;//传入资源列表
            builds[0].assetBundleName = fixName + "_" + folder + ".unity3D"; //设置ab包名称
            Debug.Log($"开始构建{relativePath},总文件数={files.Length}");
            //通过构建管线工具打包
            BuildPipeline.BuildAssetBundles(_abOutput, builds, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
        }

        static string ResetOutPut()
        {
            string outPut = _abOutput;
            if (Directory.Exists(outPut)) //每次build都先删除旧的，build最新的
            {
                Directory.Delete(outPut, true);
            }
            Directory.CreateDirectory(outPut);
            return outPut;
        }

        static void BuildByTarget(BuildTarget target)
        {
            string outPut = ResetOutPut();
            //通过构建管线 BuildPipeline 类构建所有ab包，返回的 manifest 就是所有AssetBundle 清单
            AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(outPut,
                BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle,
                target);
            EditorUtility.DisplayDialog("提示", $"构建目标平台{Enum.GetName(typeof(BuildTarget), target)}完成", "确定");
        }

    }
}
